// <com.woyouqiu.Copyright>
// --------------------------------------------------------------
// <copyright>上海有求网络科技有限公司 2015</copyright>
// <Solution>Vecen.BangChuang</Solution>
// <Project>Vecen.BangChuang.EntityFrameworkCore</Project>
// <FileName>DefaultSettingBuilder.cs</FileName>
// <CreateTime>2017-04-30 17:07</CreateTime>
// <Author>何苗</Author>
// <Email>hemiao@woyouqiu.com</Email>
// <log date="2017-04-30 17:07" version="00001">创建</log>
// --------------------------------------------------------------
// </com.woyouqiu.Copyright>

using System.Linq;
using Abp.Configuration;
using Vecen.BangChuang.Net.Sms;

namespace Vecen.BangChuang.EntityFrameworkCore.Seed.Tenants {
    public class DefaultSettingBuilder {
        private readonly BangChuangDbContext _context;
        private int _tenantId;

        public DefaultSettingBuilder(BangChuangDbContext context, int tenantId) {
            _context = context;
            _tenantId = tenantId;
        }

        public void Create() {
            //高德地图APIKEY
            AddSettingIfNotExists(BangChuangConsts.SettingsName.ThirdApi.ThirdApi_Amap_WebApi_Key,
                "23bfc54fe57ae4cc5728ea7aa3f3ad7a");

            //短信限制
            AddSettingIfNotExists(BangChuangConsts.SettingsName.Validation_Code_IntervalTime, "1");
            AddSettingIfNotExists(BangChuangConsts.SettingsName.Validation_Code_LimitCount, "20");

            //短信内容
            AddSettingIfNotExists(BangChuangConsts.SettingsName.ThirdApi.Validation_Code_Sms_Register_TemplateCode,
                "172957");
            AddSettingIfNotExists(
                BangChuangConsts.SettingsName.ThirdApi.Validation_Code_Sms_ChangePhoneNumber_TemplateCode, "0");
            AddSettingIfNotExists(BangChuangConsts.SettingsName.ThirdApi.Validation_Code_Sms_QuickLogin_TemplateCode,
                "0");
            AddSettingIfNotExists(
                BangChuangConsts.SettingsName.ThirdApi.Validation_Code_Sms_ChangePassword_TemplateCode, "172958");
            AddSettingIfNotExists(
                BangChuangConsts.SettingsName.ThirdApi.Validation_Code_Sms_SetWithdrawPassword_TemplateCode, "175461");
            AddSettingIfNotExists(SmsSettingNames.AccountId, "8a216da85bad7876015bae56652b0095");
            AddSettingIfNotExists(SmsSettingNames.AccountToken, "7bf797a905724ea284c814ee5a72198a");
            AddSettingIfNotExists(SmsSettingNames.AppKey, "8a216da85bad7876015bae566797009c");
            AddSettingIfNotExists(SmsSettingNames.AppSecret, "15c4511ad817edeec1e71b6ee61b9e7f");
            AddSettingIfNotExists(SmsSettingNames.ServiceUrl,
                "https://app.cloopen.com:8883/2013-12-26/Accounts/{accountSid}/SMS/TemplateSMS?sig={SigParameter}");


            AddSettingIfNotExists(BangChuangConsts.SettingsName.ThirdApi.ThirdApi_PingPP_LiveMode, "fales");
            AddSettingIfNotExists(BangChuangConsts.SettingsName.ThirdApi.ThirdApi_PingPP_AppId, "fales");
            AddSettingIfNotExists(BangChuangConsts.SettingsName.ThirdApi.ThirdApi_PingPP_AppKey_Live, "fales");
            AddSettingIfNotExists(BangChuangConsts.SettingsName.ThirdApi.ThirdApi_PingPP_AppKey_Test, "fales");

            AddSettingIfNotExists(BangChuangConsts.SettingsName.DisplayCostPriceForApp, "true");
            AddSettingIfNotExists(BangChuangConsts.SettingsName.DisplayPriceOnSharePage, "true");
        }

        private void AddSettingIfNotExists(string name, string value, int? tenantId = null) {
            if(_context.Settings.Any(s => s.Name == name && s.TenantId == tenantId && s.UserId == null)) {
                return;
            }

            _context.Settings.Add(new Setting(tenantId, null, name, value));
            _context.SaveChanges();
        }
    }
}